package com.zz.admin.demo.beanMapperDemo;

import com.zz.common.utils.mapper.BeanMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BeanMapperTest {

    @RequestMapping("/bean-mapper-test")
    public String beanMapperTest() {
        UserApiDto user = new UserApiDto();
        user.setName("admin");
        user.setAge(25);
        user.setEmail("admin@zz.com");
        user.setAddress("China");
        user.setBirthday(LocalDate.now());
        user.setRegisterTime(LocalDateTime.now());

        UserApiDto user1 = new UserApiDto();
        user1.setName("user1");
        user1.setAge(30);
        user1.setEmail("user1@zz.com");
        user1.setAddress("USA");

        List<UserApiDto> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user1);

        Map<String, String> attr = new HashMap<>();
        attr.put("address", "phone");

        List<User> userList1 = BeanMapper.mapList(userList, UserApiDto.class, User.class, attr);
        for (User user2 : userList1) {
            System.out.println(user2.toString());
        }


//        User map = BeanMapper.map(user, TypeFactory.valueOf(UserApiDto.class), TypeFactory.valueOf(User.class));


        User map = BeanMapper.map(user, User.class, attr);

        user.setRegisterTime(LocalDateTime.from(user.getRegisterTime()));
        System.out.println("............" + user);
        return map.toString();
    }

    @RequestMapping("/reflect-test")
    public void reflectTest() throws NoSuchFieldException {
        List<Integer> list = new ArrayList<>();
        Field field = list.getClass().getDeclaredField("elementData");
        field.setAccessible(true);
        try {
            for (int i = 0; i < 100; i++) {
                Object[] elementData = (Object[]) field.get(list);
                System.out.println(i + "," + "list size:" + list.size() + ", element array length:" + elementData.length);
                list.add(i);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}
